// --------------------------------------------------------------------------------------------------------------------
// <copyright file="TypeDescriptorProvider.TickSizeRule.cs" company="Open Trader">
//   Copyright (c) David Denis (david.denis@systemathics.com)
// </copyright>
// <summary>
//   |  Open Trader - The Open Source Systematic Trading Platform
//   |
//   |  This program is free software: you can redistribute it and/or modify
//   |  it under the terms of the GNU General Public License as published by
//   |  the Free Software Foundation, either version 2 of the License, or
//   |  (at your option) any later version.
//   |
//   |  This program is distributed in the hope that it will be useful,
//   |  but WITHOUT ANY WARRANTY; without even the implied warranty of
//   |  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   |  GNU General Public License for more details.
//   |
//   |  You should have received a copy of the GNU General Public License
//   |  along with this program.  If not, see http://www.gnu.org/licenses
//   |
//   |  Up to date informations about Open Trader can be found at :
//   |    http://opentrader.org
//   |    http://opentrader.codeplex.com
//   |
//   |  For professional services, please visit us at :
//   |    http://www.systemathics.com
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Org.OpenTrader.Framework.Forge
{
    #region Using Directives

    using System;
    using System.ComponentModel;

    using Org.OpenTrader.Framework.Forge.Interfaces;

    #endregion

    /// <summary>
    /// The tick size rule type description provider.
    /// </summary>
    public class TickSizeRuleTypeDescriptionProvider : System.ComponentModel.TypeDescriptionProvider
    {
        #region Constants and Fields

        /// <summary>
        /// The tsr.
        /// </summary>
        private readonly ITickSizeRule tsr;

        #endregion

        #region Constructors and Destructors

        /// <summary>
        /// Initializes static members of the <see cref="TickSizeRuleTypeDescriptionProvider"/> class.
        /// </summary>
        static TickSizeRuleTypeDescriptionProvider()
        {
            // also for ITickSizeRule !
            TypeDescriptor.AddProvider(new TickSizeRuleTypeDescriptionProvider(), typeof(ITickSizeRule));
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="TickSizeRuleTypeDescriptionProvider"/> class.
        /// </summary>
        public TickSizeRuleTypeDescriptionProvider()
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="TickSizeRuleTypeDescriptionProvider"/> class.
        /// </summary>
        /// <param name="tsr">
        /// The tsr.
        /// </param>
        public TickSizeRuleTypeDescriptionProvider(ITickSizeRule tsr)
        {
            this.tsr = tsr;
        }

        #endregion

        #region Public Methods

        /// <summary>
        /// The get type descriptor.
        /// </summary>
        /// <param name="objectType">
        /// The object type.
        /// </param>
        /// <param name="tsr">
        /// The tsr.
        /// </param>
        /// <returns>
        /// </returns>
        public override ICustomTypeDescriptor GetTypeDescriptor(Type objectType, object tsr)
        {
            return new MyTypeDescriptor(this.tsr);
        }

        #endregion

        // internal class MyEditor : UITypeEditor
        // {
        // private readonly ITickSizeRule tsr;
        // public MyEditor(ITickSizeRule tsr)
        // {
        // tsr = tsr;
        // }

        // public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
        // {
        // return UITypeEditorEditStyle.Modal;
        // }

        // // Displays the UI for value selection.
        // public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        // {
        // IWindowsFormsEditorService editorService = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
        // if (editorService != null)
        // {
        // object initialValue = value;
        // bool cancelled = false;

        // //T wiring = (T)value;
        // //UIWiringEditor ctrl = new UIWiringEditor(tsr, wiring);
        // //ctrl.ApplyButton.Click += (object sender, EventArgs e) =>
        // //{
        // //    editorService.CloseDropDown();

        // //};
        // //ctrl.CancelButton.Click += (object sender, EventArgs e) =>
        // //{
        // //    editorService.CloseDropDown();
        // //    cancelled = true;
        // //};
        // //editorService.DropDownControl(ctrl);

        // //if (cancelled)
        // //    return initialValue;

        // //if( ctrl.Wiring != null )
        // //    return ctrl.Wiring.Clone(); // Ensure that the { set; } is called !

        // //return ctrl.Wiring;
        // }

        // return value;
        // }
        // }

        /// <summary>
        /// The my converter.
        /// </summary>
        internal class MyConverter : StringConverter
        {
            #region Constants and Fields

            /// <summary>
            /// The tsr.
            /// </summary>
            private readonly ITickSizeRule tsr;

            #endregion

            #region Constructors and Destructors

            /// <summary>
            /// Initializes a new instance of the <see cref="MyConverter"/> class.
            /// </summary>
            /// <param name="tsr">
            /// The tsr.
            /// </param>
            public MyConverter(ITickSizeRule tsr)
            {
                this.tsr = tsr;
            }

            #endregion

            #region Public Methods

            /// <summary>
            /// The convert to.
            /// </summary>
            /// <param name="context">
            /// The context.
            /// </param>
            /// <param name="culture">
            /// The culture.
            /// </param>
            /// <param name="value">
            /// The value.
            /// </param>
            /// <param name="destinationType">
            /// The destination type.
            /// </param>
            /// <returns>
            /// The convert to.
            /// </returns>
            public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
            {
                var tsr = value as ITickSizeRule;
                if (tsr != null)
                {
                    return tsr.Name;
                }

                return string.Empty;
            }

            #endregion
        }

        /// <summary>
        /// The my type descriptor.
        /// </summary>
        internal class MyTypeDescriptor : CustomTypeDescriptor
        {
            #region Constants and Fields

            /// <summary>
            /// The tsr.
            /// </summary>
            private readonly ITickSizeRule tsr;

            #endregion

            #region Constructors and Destructors

            /// <summary>
            /// Initializes a new instance of the <see cref="MyTypeDescriptor"/> class.
            /// </summary>
            /// <param name="tsr">
            /// The tsr.
            /// </param>
            public MyTypeDescriptor(ITickSizeRule tsr)
            {
                this.tsr = tsr;
            }

            #endregion

            // public override object GetEditor(Type editorBaseType)
            // {
            // return new MyEditor(tsr);
            // }
            #region Public Methods

            /// <summary>
            /// The get converter.
            /// </summary>
            /// <returns>
            /// </returns>
            public override TypeConverter GetConverter()
            {
                return new MyConverter(this.tsr);
            }

            #endregion
        }
    }
}